package com.map;

import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import com.essential.R;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

public class MapExampleActivity extends MapActivity {
	MapView m_mapView;
	MapController m_mapController;
	GeoPoint m_curLocation;
	String m_locationProvider;
	LocationManager m_locationMgr;
	List<Overlay> m_overlayController;
	boolean m_locationLoopActive = false;
	Geocoder m_geocoder;

	String m_gmapApiKey = "0Xr4bFCkKzrQpbupypILGJSTa0_VIGdjAkLD1sQ";

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		m_mapView = new MapView(this, m_gmapApiKey);
		m_mapController = m_mapView.getController();
		m_overlayController = m_mapView.getOverlays();
		m_mapController.setZoom(9);

		buildGPS();
		m_geocoder = new Geocoder(this, Locale.getDefault());

		setContentView(m_mapView);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private void buildGPS() {
		List<String> providerList = null;
		Criteria c = new Criteria();
		c.setAccuracy(Criteria.ACCURACY_FINE);
		c.setAltitudeRequired(false);
		c.setBearingRequired(false);
		c.setCostAllowed(false);
		c.setSpeedRequired(false);
		c.setPowerRequirement(Criteria.POWER_LOW);

		m_locationMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		m_locationProvider = m_locationMgr.getBestProvider(c, true);

		if (m_locationProvider != null) {
			return;
		}

		providerList = m_locationMgr.getProviders(true);

		if (providerList.size() > 0) {
			m_locationProvider = providerList.get(0);
		}
	}

	boolean running = true;

	private void startLocationThread() {
		try {
			LocationUpdater l = new LocationUpdater();
			registerReceiver(l, new IntentFilter("GPS_UPDATE"));

			m_locationMgr.requestLocationUpdates(m_locationProvider, 5000, 50,
					PendingIntent.getBroadcast(MapExampleActivity.this, 0,
							new Intent("GPS_UPDATE"), 0));
		} catch (Exception e) {
		}
	}

	class LocationUpdater extends BroadcastReceiver {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			Location here;

			if (m_locationProvider == null) {
				here = m_locationMgr.getLastKnownLocation("gps");
			} else {
				here = m_locationMgr.getLastKnownLocation(m_locationProvider);
			}

			if (null == here)
				return;
			setMapLocationCenter(here.getLatitude(), here.getLongitude());
		}

	}

	public void setMapLocationCenter(double lat, double lon) {
		Double geoLat = lat * 1E6;
		Double geoLon = lon * 1E6;
		m_curLocation = new GeoPoint(geoLat.intValue(), geoLon.intValue());
		m_mapController.animateTo(m_curLocation);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		super.onKeyDown(keyCode, event);
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_CENTER:
			if (!m_locationLoopActive) {
				m_locationLoopActive = true;
				startLocationThread();
				return true;
			}
			break;
		}
		return false;
	}

	class TackOverlay extends Overlay {

		MapExampleActivity ctx;
		Bitmap tack;

		TackOverlay(MapExampleActivity c) {
			super();
			BitmapDrawable b = (BitmapDrawable) c.getResources().getDrawable(
					R.drawable.ball_bmp);
			tack = b.getBitmap();
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			try {
				if (m_curLocation == null) {
					return;
				}

				int tackX = m_curLocation.getLatitudeE6()
						- (tack.getWidth() / 2);
				int tackY = m_curLocation.getLongitudeE6() - (tack.getHeight());
				canvas.drawBitmap(tack, tackX, tackY, new Paint());
			} catch (Exception e) {
				Log.e(getLocalClassName(), "Crap!");
			}
		}
	}

}
